package com.wyvoid.core.mvc.stripes.validation;

import net.sourceforge.stripes.validation.ScopedLocalizableError;
import net.sourceforge.stripes.validation.TypeConverter;
import net.sourceforge.stripes.validation.ValidationError;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

/**
 * User: cjp
 * Date: 2006-11-5
 * Time: 18:57:00
 */
public class TimeTypeConverter implements TypeConverter<Date> {
    private Locale locale;

    public void setLocale(Locale locale) {
        this.locale = locale;
    }
    public Date convert(String input,
                        Class<? extends Date> targetType,
                        Collection<ValidationError> errors) {

        SimpleDateFormat dataFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;

            try {
                date = dataFormat.parse(input);
            }
            catch (ParseException pe) {}

        if (date != null) {
            return date;
        } else {
            errors.add(new ScopedLocalizableError("converter.date", "invalidDate"));
            return null;
        }
    }
}
